import { defineConfig, type Plugin } from 'vite'
import react from '@vitejs/plugin-react-swc'
// @ts-ignore 忽略类型检查错误
import mockjs from 'mockjs'
import url from 'node:url'

const viteMockServer = (): Plugin => {
  return {
    name: 'vite-mock-server',
    configureServer(server) {
      server.middlewares.use('/api/list', (req,res) => {
        const parseUrl = url.parse(req.originalUrl,true).query
        res.setHeader('Content-Type','application/json')
        const data = mockjs.mock({
          'list|10': [{
            'id|+1': 1,
            'name': parseUrl.keyWord,
            'address': '@county(true)',
          }]
        })
        // console.log('data',data);
        
        res.end(JSON.stringify(data))
      })
    }
  }
}

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(),
    viteMockServer()
  ],
  css: {
    modules: {
      localsConvention: 'dashes',
      generateScopedName: '[name]__[local]___[hash:base64:5]'
    }
  }
})
